package com.ayoka.parenting.ui.group.topic.detail;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.BaseResponse;
import com.ayoka.parenting.bean.CommentBean;
import com.ayoka.parenting.bean.PagerBean;
import com.ayoka.parenting.interf.SimpleRequestCallback;
import com.ayoka.parenting.mvp.BasePresenterImpl;
import com.jincp.common.utils.UIUtils;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: TopicDetailPresenter.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class TopicDetailPresenter extends BasePresenterImpl<TopicDetailContract.View> implements TopicDetailContract.Presenter {

    private TopicDetailModel model;

    public TopicDetailPresenter() {
        model = new TopicDetailModel();
    }

    @Override
    public void getTopicCommentListData(boolean isShowWaitDialog, String topicId, int pageNo, int pageSize) {
        if (isShowWaitDialog) {
            mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        }
        model.getTopicCommentListData(this, topicId, pageNo, pageSize, new SimpleRequestCallback<PagerBean<CommentBean>>() {
            @Override
            public void onSuccess(PagerBean<CommentBean> data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().configTopicCommentListData(data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
            }
        });
    }

    @Override
    public void commentTopic(String replyToUserId, String replyToTopicID, String topicId, String commentContent) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.commentTopic(this, replyToUserId, replyToTopicID, topicId, commentContent, new SimpleRequestCallback<CommentBean>() {
            @Override
            public void onSuccess(CommentBean data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onCommentTopicCallback(true, data);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onCommentTopicCallback(false, null);
            }
        });
    }

    @Override
    public void praiseComment(String commentId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.praiseComment(this, commentId, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onPraiseCommentCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onPraiseCommentCallback(false, position);
            }
        });
    }

    @Override
    public void cancelPraiseComment(String commentId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.cancelPraiseComment(this, commentId, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onCancelPraiseCommentCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onCancelPraiseCommentCallback(false, position);
            }
        });
    }

    @Override
    public void starComment(String commentId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.starComment(this, commentId, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onStarCommentCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onStarCommentCallback(false, position);
            }
        });
    }

    @Override
    public void cancelStarComment(String commentId, final int position) {
        mView.get().showWaitDialog(UIUtils.getString(R.string.loading));
        model.cancelStarComment(this, commentId, new SimpleRequestCallback<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse data) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().onCancelStarCommentCallback(true, position);
            }

            @Override
            public void onFailure(int errCode, String errMsg) {
                if (!isAttached()) return;
                mView.get().hideWaitDialog();
                mView.get().showToast(errMsg);
                mView.get().onCancelStarCommentCallback(false, position);
            }
        });
    }
}
